package com.seewo.dubbo.debugger.admin.entity;

import com.alibaba.fastjson.annotation.JSONField;
import com.baomidou.mybatisplus.annotations.TableField;
import com.baomidou.mybatisplus.annotations.TableId;
import com.baomidou.mybatisplus.annotations.TableName;
import com.baomidou.mybatisplus.enums.IdType;
import com.seewo.dubbo.debugger.admin.common.validate.group.Query;
import com.seewo.dubbo.debugger.admin.common.validate.group.Update;
import lombok.Data;
import lombok.experimental.Accessors;
import org.hibernate.validator.constraints.NotBlank;

import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
import java.io.Serializable;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 *
 * </p>
 *
 * @author chenliangliang
 * @since 2018-07-25
 */
@Data
@Accessors(chain = true)
@TableName("t_service")
public class Service implements Serializable {

    private static final long serialVersionUID = 455332675166886556L;

    @TableId(value = "c_id", type = IdType.AUTO)
    @NotNull(message = "服务id不能为空", groups = {Update.class})
    @Min(message = "cid不合法", value = 0, groups = {Update.class})
    private Integer cId;
    @NotNull(message = "注册中心id不能为空", groups = {Query.class})
    private Integer cRegistryId;
    /**
     * 服务名
     */
    private String cName;

    @NotBlank(message = "协议不能为空", groups = {Query.class})
    private String cProtocol;
    /**
     * dubbo服务地址
     */
    @NotBlank(message = "链接地址不能为空", groups = {Query.class})
//    @Pattern(regexp = "(\\d|[1-9]\\d|1\\d{2}|2[0-5][0-5])\\.(\\d|[1-9]\\d|1\\d{2}|2[0-5][0-5])\\.(\\d|[1-9]\\d|1\\d{2}|2[0-5][0-5])\\.(\\d|[1-9]\\d|1\\d{2}|2[0-5][0-5]):([0-9]|[1-9]\\d{1,3}|[1-5]\\d{4}|6[0-5]{2}[0-3][0-5])",
//            message = "ip:port格式错误",groups = {Query.class})
    private String cAddress;
    /**
     * 描述
     */
    @NotBlank(message = "要更新的描述不能为空", groups = {Update.class})
    private String cDesc;
    private Integer cDebug;
    @JSONField(serialize = false, deserialize = false)
    private Date cCreateTime;
    @JSONField(serialize = false, deserialize = false)
    private Date cUpdateTime;

    @TableField(exist = false)
    private List<Interface> interfaces;


    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof Service)) return false;
        Service service = (Service) o;
        return Objects.equals(cRegistryId, service.cRegistryId) &&
                Objects.equals(cProtocol, service.cProtocol) &&
                Objects.equals(cAddress, service.cAddress);
    }

    @Override
    public int hashCode() {

        return Objects.hash(cRegistryId, cProtocol, cAddress);
    }
}
